package com.pricl.wechat.faq.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.soecode.wxtools.exception.WxErrorException;
import com.soecode.wxtools.util.StringUtils;

/**
 * 微信授权开放平台版
 *
 */
public class MyWxConfig {
	private static final String configFile = "/oauth2.properties";
	private static MyWxConfig config = null;
	
	//配置文件读取项
	private volatile String appId;
	private volatile String appSecret;
	private volatile String redirecturl;
	
	public MyWxConfig() {
		//写读配置文件代码
		Properties p = new Properties();
		InputStream inStream = this.getClass().getResourceAsStream(configFile);
		if(inStream == null){
			try {
				throw new WxErrorException("根目录找不到文件");
			} catch (WxErrorException e) {
				e.printStackTrace();
			}
		}
		try {
			p.load(inStream);
			this.appId = p.getProperty("wx.appId");
			if (StringUtils.isNotBlank(this.appId))
				this.appId = this.appId.trim();
			this.appSecret = p.getProperty("wx.appSecret");
			if (StringUtils.isNotBlank(this.appSecret))
				this.appSecret = this.appSecret.trim();
			this.redirecturl = p.getProperty("wx.redirecturl");
			if (StringUtils.isNotBlank(this.redirecturl))
				this.redirecturl = this.redirecturl.trim();
			inStream.close();
		} catch (IOException e) {
			try {
				throw new WxErrorException("load oauth2.properties error,class根目录下找不到wx.properties文件");
			} catch (WxErrorException e1) {
				e1.printStackTrace();
			}
		}
		System.out.println("load oauth2.properties success");
	}
	
	/**
	 * 同步获取/加载单例
	 * @return
	 */
	public static synchronized MyWxConfig getInstance(){
		if(config == null){
			config = new MyWxConfig();
		}
		return config;
	}

	public static MyWxConfig getConfig() {
		return config;
	}

	public static void setConfig(MyWxConfig config) {
		MyWxConfig.config = config;
	}

	public String getAppId() {
		return appId;
	}

	public void setAppId(String appId) {
		this.appId = appId;
	}

	public String getAppSecret() {
		return appSecret;
	}

	public void setAppSecret(String appSecret) {
		this.appSecret = appSecret;
	}

	public String getRedirecturl() {
		return redirecturl;
	}

	public void setRedirecturl(String redirecturl) {
		this.redirecturl = redirecturl;
	}

	public static String getConfigfile() {
		return configFile;
	}
	

}
